/* 
 *  Licensed to the Apache Software Foundation (ASF) under one or more 
 *  contributor license agreements.  See the NOTICE file distributed with 
 *  this work for additional information regarding copyright ownership. 
 *  The ASF licenses this file to You under the Apache License, Version 2.0 
 *  (the "License"); you may not use this file except in compliance with 
 *  the License.  You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 * 
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
/* Generated By:JavaCC: Do not edit this line. LdapUrlParser.java */

package org.apache.harmony.jndi.provider.ldap.parser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.io.StringReader;
import javax.naming.directory.SearchControls;
import org.apache.harmony.jndi.provider.ldap.Filter;
import org.apache.harmony.jndi.provider.ldap.asn1.Utils;

public class LdapUrlParser implements LdapUrlParserConstants {
    private SearchControls controls;

    private Filter filter;

    private String baseObject = "";

    private String host = DEFAULT_HOST;

    private int port = DEFAULT_PORT;

    private boolean hasAttributes = false;

    private boolean hasScope = false;

    private boolean hasFilter = false;

    private boolean hasExtensions= false;

    private boolean isEndEOF = false;

    private static final int DEFAULT_PORT = 389;

    private static final int DEFAULT_SSL_PORT = 636;

    private static final String DEFAULT_HOST = "localhost";

    public LdapUrlParser(String url) {
        this(new StringReader(url));
        isEndEOF = true;
    }

    public SearchControls getControls() {
        return controls;
    }

    public Filter getFilter() {
        return filter;
    }

    private String convertToUtf8Char(String s) {
        byte[] bs = new byte[] { (byte)Integer.parseInt(s, 16) };
        return Utils.getString(bs);
    }

    /**
     * get the host part of the url, if host part is omitted, <code>null</code>
     * will be return
     * 
     * @return host part of the url or <code>null</code> if it's omitted
     */
    public String getHost() {
        return host;
    }

    /**
     * get the port number of the url, if this part is omitted, -1 will be
     * return
     * 
     * @return port number of the url or -1 if it's omitted
     */
    public int getPort() {
        return port;
    }

    public String getBaseObject() {
        return baseObject;
    }

    public boolean hasFilter() {
        return hasFilter;
    }

    public boolean hasAttributes() {
        return hasAttributes;
    }

    public boolean hasScope() {
        return hasScope;
    }

    public boolean hasExtensions() {
        return hasExtensions;
    }

    public static void main(String args[]) throws ParseException, FileNotFoundException {
        LdapUrlParser parser = new LdapUrlParser(new FileInputStream("parser.url.test"));
//        URLParser parser = new URLParser(System.in);
        //  FilterParser parser = new FilterParser(System.in);
        //System.out.println(parser.value());
        parser.test();
        //parser.value();
    }

  final public void parseURL() throws ParseException {
            Token t;
    t = jj_consume_token(SCHEME);
                    if (t.image.equals("ldaps://")) {
                        port = DEFAULT_SSL_PORT;
                    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SCHEME:
    case PRE_XTOKEN:
    case SCOPE:
    case PERCENT:
    case PERIOD:
    case EXCLAM_MARK:
    case EQUAL:
    case HEX_CHAR:
    case NOHEX_CHAR:
    case DIGIT:
    case ZERO:
    case CHAR:
      hostport();
      break;
    default:
      jj_la1[0] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SLASH:
      jj_consume_token(SLASH);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SCHEME:
      case PRE_XTOKEN:
      case SCOPE:
      case PERCENT:
      case PERIOD:
      case EXCLAM_MARK:
      case EQUAL:
      case HEX_CHAR:
      case NOHEX_CHAR:
      case DIGIT:
      case ZERO:
      case CHAR:
        dn();
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case QUESTION_MARK:
          jj_consume_token(QUESTION_MARK);
          attributes();
          break;
        default:
          jj_la1[1] = jj_gen;
          ;
        }
        break;
      default:
        jj_la1[2] = jj_gen;
        ;
      }
      break;
    default:
      jj_la1[3] = jj_gen;
      ;
    }
                if (isEndEOF) {
                    eof();
                }
  }

  final public void eof() throws ParseException {
    jj_consume_token(0);
  }

  final public String number() throws ParseException {
            StringBuilder value = new StringBuilder();
            Token t;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case ZERO:
      t = jj_consume_token(ZERO);
                         value.append(t.image);
      break;
    case DIGIT:
      t = jj_consume_token(DIGIT);
                            value.append(t.image);
      label_1:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case DIGIT:
        case ZERO:
          ;
          break;
        default:
          jj_la1[4] = jj_gen;
          break label_1;
        }
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case ZERO:
          t = jj_consume_token(ZERO);
                            value.append(t.image);
          break;
        case DIGIT:
          t = jj_consume_token(DIGIT);
                             value.append(t.image);
          break;
        default:
          jj_la1[5] = jj_gen;
          jj_consume_token(-1);
          throw new ParseException();
        }
      }
      break;
    default:
      jj_la1[6] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
             {if (true) return value.toString();}
    throw new Error("Missing return statement in function");
  }

  final public String hexDigit() throws ParseException {
            String value;
            Token t;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case HEX_CHAR:
      t = jj_consume_token(HEX_CHAR);
      break;
    case DIGIT:
      t = jj_consume_token(DIGIT);
      break;
    case ZERO:
      t = jj_consume_token(ZERO);
      break;
    default:
      jj_la1[7] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
                                                          value = t.image;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case HEX_CHAR:
      t = jj_consume_token(HEX_CHAR);
      break;
    case DIGIT:
      t = jj_consume_token(DIGIT);
      break;
    case ZERO:
      t = jj_consume_token(ZERO);
      break;
    default:
      jj_la1[8] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
                                                          value += t.image;
             {if (true) return value;}
    throw new Error("Missing return statement in function");
  }

  final public String value() throws ParseException {
            StringBuilder value = new StringBuilder();
            String ch;
    label_2:
    while (true) {
      ch = ch();
                        value.append(ch);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SCHEME:
      case PRE_XTOKEN:
      case SCOPE:
      case PERCENT:
      case PERIOD:
      case EXCLAM_MARK:
      case EQUAL:
      case HEX_CHAR:
      case NOHEX_CHAR:
      case DIGIT:
      case ZERO:
      case CHAR:
        ;
        break;
      default:
        jj_la1[9] = jj_gen;
        break label_2;
      }
    }
             {if (true) return value.toString();}
    throw new Error("Missing return statement in function");
  }

  final public String ch() throws ParseException {
            Token t;
            String value;
            String temp;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case CHAR:
      t = jj_consume_token(CHAR);
                         value = t.image;
      break;
    case PERCENT:
      t = jj_consume_token(PERCENT);
      value = hexDigit();
                                                 value = convertToUtf8Char(value);
      break;
    case ZERO:
      t = jj_consume_token(ZERO);
                           value = t.image;
      break;
    case DIGIT:
      t = jj_consume_token(DIGIT);
                            value = t.image;
      break;
    case HEX_CHAR:
      t = jj_consume_token(HEX_CHAR);
                               value = t.image;
      break;
    case NOHEX_CHAR:
      t = jj_consume_token(NOHEX_CHAR);
                                 value = t.image;
      break;
    case SCHEME:
      t = jj_consume_token(SCHEME);
                             value = t.image;
      break;
    case EQUAL:
      t = jj_consume_token(EQUAL);
                            value = t.image;
      break;
    case PERIOD:
      t = jj_consume_token(PERIOD);
                             value = t.image;
      break;
    case EXCLAM_MARK:
      t = jj_consume_token(EXCLAM_MARK);
                                  value = t.image;
      break;
    case SCOPE:
      t = jj_consume_token(SCOPE);
                            value = t.image;
      break;
    case PRE_XTOKEN:
      t = jj_consume_token(PRE_XTOKEN);
                                 value = t.image;
      break;
    default:
      jj_la1[10] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
             {if (true) return value;}
    throw new Error("Missing return statement in function");
  }

  final public void hostport() throws ParseException {
            String ch;
            StringBuilder h = new StringBuilder();
    label_3:
    while (true) {
      ch = ch();
                        h.append(ch);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case SCHEME:
      case PRE_XTOKEN:
      case SCOPE:
      case PERCENT:
      case PERIOD:
      case EXCLAM_MARK:
      case EQUAL:
      case HEX_CHAR:
      case NOHEX_CHAR:
      case DIGIT:
      case ZERO:
      case CHAR:
        ;
        break;
      default:
        jj_la1[11] = jj_gen;
        break label_3;
      }
    }
                                          host = h.toString();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case COLON:
      jj_consume_token(COLON);
      ch = number();
                                    port = Integer.valueOf(ch);
      break;
    default:
      jj_la1[12] = jj_gen;
      ;
    }
  }

  final public void dn() throws ParseException {
            Token t;
            String value;
            StringBuilder dn = new StringBuilder();
    value = value();
                              dn.append(value);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case COMMA:
      label_4:
      while (true) {
        t = jj_consume_token(COMMA);
                           dn.append(t.image);
        value = value();
                                                                 dn.append(value);
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case COMMA:
          ;
          break;
        default:
          jj_la1[13] = jj_gen;
          break label_4;
        }
      }
      break;
    default:
      jj_la1[14] = jj_gen;
      ;
    }
                baseObject = dn.toString();
  }

  final public void attributes() throws ParseException {
            String value;
            List attrs = new ArrayList();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SCHEME:
    case PRE_XTOKEN:
    case SCOPE:
    case PERCENT:
    case PERIOD:
    case EXCLAM_MARK:
    case EQUAL:
    case HEX_CHAR:
    case NOHEX_CHAR:
    case DIGIT:
    case ZERO:
    case CHAR:
      value = value();
                              attrs.add(value);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case COMMA:
        label_5:
        while (true) {
          jj_consume_token(COMMA);
          value = value();
                                        attrs.add(value);
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case COMMA:
            ;
            break;
          default:
            jj_la1[15] = jj_gen;
            break label_5;
          }
        }
        break;
      default:
        jj_la1[16] = jj_gen;
        ;
      }
      break;
    default:
      jj_la1[17] = jj_gen;
      ;
    }
                if (attrs.size() != 0) {
                    hasAttributes = true;
                    if (controls == null) {
                        // FIXME: test what default search parameter value is
                        controls = new SearchControls();
                    }
                    controls.setReturningAttributes((String[]) attrs.toArray(new String[0]));
                }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case QUESTION_MARK:
      jj_consume_token(QUESTION_MARK);
      scope();
      break;
    default:
      jj_la1[18] = jj_gen;
      ;
    }
  }

  final public void attrDescr() throws ParseException {
    value();
  }

  final public void scope() throws ParseException {
            Token t;
            String scope;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SCOPE:
      t = jj_consume_token(SCOPE);
                    scope = t.image;
                    hasScope = true;
                    if (controls == null) {
                        controls = new SearchControls();
                    }
                    if (scope.equals("base")) {
                        controls.setSearchScope(SearchControls.OBJECT_SCOPE);
                    } else if (scope.equals("one")) {
                        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
                    } else {
                        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    }
      break;
    default:
      jj_la1[19] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case QUESTION_MARK:
      jj_consume_token(QUESTION_MARK);
      filter();
      break;
    default:
      jj_la1[20] = jj_gen;
      ;
    }
  }

  final public void filter() throws ParseException {
            String value;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SCHEME:
    case PRE_XTOKEN:
    case SCOPE:
    case PERCENT:
    case PERIOD:
    case EXCLAM_MARK:
    case EQUAL:
    case HEX_CHAR:
    case NOHEX_CHAR:
    case DIGIT:
    case ZERO:
    case CHAR:
      value = value();
                    FilterParser parser = new FilterParser(new StringReader(value));
                    filter = parser.parse();
                    hasFilter = true;
      break;
    default:
      jj_la1[21] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case QUESTION_MARK:
      jj_consume_token(QUESTION_MARK);
      extensions();
      break;
    default:
      jj_la1[22] = jj_gen;
      ;
    }
  }

  final public void extensions() throws ParseException {
    extension();
                         hasExtensions = true;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case COMMA:
      label_6:
      while (true) {
        jj_consume_token(COMMA);
        extension();
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case COMMA:
          ;
          break;
        default:
          jj_la1[23] = jj_gen;
          break label_6;
        }
      }
      break;
    default:
      jj_la1[24] = jj_gen;
      ;
    }
  }

  final public void extension() throws ParseException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case EXCLAM_MARK:
      jj_consume_token(EXCLAM_MARK);
      break;
    default:
      jj_la1[25] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case DIGIT:
    case ZERO:
      oid();
      break;
    case PRE_XTOKEN:
      jj_consume_token(PRE_XTOKEN);
      oid();
      break;
    default:
      jj_la1[26] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case EQUAL:
      jj_consume_token(EQUAL);
      value();
      break;
    default:
      jj_la1[27] = jj_gen;
      ;
    }
  }

  final public String oid() throws ParseException {
            StringBuilder value = new StringBuilder();
            String number = null;
            Token t = null;
    number = number();
                               value.append(number);
    label_7:
    while (true) {
      t = jj_consume_token(PERIOD);
                               value.append(t.image);
      number = number();
                                    value.append(number);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case PERIOD:
        ;
        break;
      default:
        jj_la1[28] = jj_gen;
        break label_7;
      }
    }
             {if (true) return value.toString();}
    throw new Error("Missing return statement in function");
  }

  final public void test() throws ParseException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case SCHEME:
      parseURL();
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 17:
        jj_consume_token(17);
        break;
      case 18:
        jj_consume_token(18);
        break;
      default:
        jj_la1[29] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      test();
      break;
    default:
      jj_la1[32] = jj_gen;
      if (jj_2_1(2)) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case 17:
          jj_consume_token(17);
          break;
        case 18:
          jj_consume_token(18);
          break;
        default:
          jj_la1[30] = jj_gen;
          jj_consume_token(-1);
          throw new ParseException();
        }
      } else {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case 17:
        case 18:
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case 17:
            jj_consume_token(17);
            break;
          case 18:
            jj_consume_token(18);
            break;
          default:
            jj_la1[31] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
          }
          jj_consume_token(0);
          break;
        case 0:
          jj_consume_token(0);
          break;
        default:
          jj_la1[33] = jj_gen;
          jj_consume_token(-1);
          throw new ParseException();
        }
      }
    }
  }

  private boolean jj_2_1(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    try { return !jj_3_1(); }
    catch(LookaheadSuccess ls) { return true; }
    finally { jj_save(0, xla); }
  }

  private boolean jj_3_1() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_scan_token(17)) {
    jj_scanpos = xsp;
    if (jj_scan_token(18)) return true;
    }
    return false;
  }

  /** Generated Token Manager. */
  public LdapUrlParserTokenManager token_source;
  SimpleCharStream jj_input_stream;
  /** Current token. */
  public Token token;
  /** Next token. */
  public Token jj_nt;
  private int jj_ntk;
  private Token jj_scanpos, jj_lastpos;
  private int jj_la;
  private int jj_gen;
  final private int[] jj_la1 = new int[34];
  static private int[] jj_la1_0;
  static {
      jj_la1_init_0();
   }
   private static void jj_la1_init_0() {
      jj_la1_0 = new int[] {0x1fe8e,0x20,0x1fe8e,0x40,0xc000,0xc000,0xc000,0xd000,0xd000,0x1fe8e,0x1fe8e,0x1fe8e,0x100,0x10,0x10,0x10,0x10,0x1fe8e,0x20,0x8,0x20,0x1fe8e,0x20,0x10,0x10,0x400,0xc004,0x800,0x200,0x60000,0x60000,0x60000,0x2,0x60001,};
   }
  final private JJCalls[] jj_2_rtns = new JJCalls[1];
  private boolean jj_rescan = false;
  private int jj_gc = 0;

  /** Constructor with InputStream. */
  public LdapUrlParser(java.io.InputStream stream) {
     this(stream, null);
  }
  /** Constructor with InputStream and supplied encoding */
  public LdapUrlParser(java.io.InputStream stream, String encoding) {
    try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source = new LdapUrlParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream) {
     ReInit(stream, null);
  }
  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream, String encoding) {
    try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  /** Constructor. */
  public LdapUrlParser(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new LdapUrlParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  /** Reinitialise. */
  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  /** Constructor with generated Token Manager. */
  public LdapUrlParser(LdapUrlParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  /** Reinitialise. */
  public void ReInit(LdapUrlParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 34; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  final private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      if (++jj_gc > 100) {
        jj_gc = 0;
        for (int i = 0; i < jj_2_rtns.length; i++) {
          JJCalls c = jj_2_rtns[i];
          while (c != null) {
            if (c.gen < jj_gen) c.first = null;
            c = c.next;
          }
        }
      }
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  static private final class LookaheadSuccess extends java.lang.Error { }
  final private LookaheadSuccess jj_ls = new LookaheadSuccess();
  final private boolean jj_scan_token(int kind) {
    if (jj_scanpos == jj_lastpos) {
      jj_la--;
      if (jj_scanpos.next == null) {
        jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
      } else {
        jj_lastpos = jj_scanpos = jj_scanpos.next;
      }
    } else {
      jj_scanpos = jj_scanpos.next;
    }
    if (jj_rescan) {
      int i = 0; Token tok = token;
      while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
      if (tok != null) jj_add_error_token(kind, i);
    }
    if (jj_scanpos.kind != kind) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
    return false;
  }


/** Get the next Token. */
  final public Token getNextToken() {
    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

/** Get the specific Token. */
  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  final private int jj_ntk() {
    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  private java.util.List jj_expentries = new java.util.ArrayList();
  private int[] jj_expentry;
  private int jj_kind = -1;
  private int[] jj_lasttokens = new int[100];
  private int jj_endpos;

  private void jj_add_error_token(int kind, int pos) {
    if (pos >= 100) return;
    if (pos == jj_endpos + 1) {
      jj_lasttokens[jj_endpos++] = kind;
    } else if (jj_endpos != 0) {
      jj_expentry = new int[jj_endpos];
      for (int i = 0; i < jj_endpos; i++) {
        jj_expentry[i] = jj_lasttokens[i];
      }
      jj_entries_loop: for (java.util.Iterator it = jj_expentries.iterator(); it.hasNext();) {
        int[] oldentry = (int[])(it.next());
        if (oldentry.length == jj_expentry.length) {
          for (int i = 0; i < jj_expentry.length; i++) {
            if (oldentry[i] != jj_expentry[i]) {
              continue jj_entries_loop;
            }
          }
          jj_expentries.add(jj_expentry);
          break jj_entries_loop;
        }
      }
      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
    }
  }

  /** Generate ParseException. */
  public ParseException generateParseException() {
    jj_expentries.clear();
    boolean[] la1tokens = new boolean[19];
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 34; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 19; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.add(jj_expentry);
      }
    }
    jj_endpos = 0;
    jj_rescan_token();
    jj_add_error_token(0, 0);
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.get(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  /** Enable tracing. */
  final public void enable_tracing() {
  }

  /** Disable tracing. */
  final public void disable_tracing() {
  }

  final private void jj_rescan_token() {
    jj_rescan = true;
    for (int i = 0; i < 1; i++) {
    try {
      JJCalls p = jj_2_rtns[i];
      do {
        if (p.gen > jj_gen) {
          jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
          switch (i) {
            case 0: jj_3_1(); break;
          }
        }
        p = p.next;
      } while (p != null);
      } catch(LookaheadSuccess ls) { }
    }
    jj_rescan = false;
  }

  final private void jj_save(int index, int xla) {
    JJCalls p = jj_2_rtns[index];
    while (p.gen > jj_gen) {
      if (p.next == null) { p = p.next = new JJCalls(); break; }
      p = p.next;
    }
    p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  }

  static final class JJCalls {
    int gen;
    Token first;
    int arg;
    JJCalls next;
  }

}
